package com.ssy.lingxi.marketing.utils;

import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.response.ResponseCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.beanvalidation.SpringValidatorAdapter;

import javax.validation.Validation;
import javax.validation.Validator;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * 参数校验工具
 * @author ds, yzc
 * @version 2.0.0
 * @date 2021/7/24
 */
public class ValidatorUtil {

    private ValidatorUtil(){}

    private static final Logger LOGGER = LoggerFactory.getLogger(ValidatorUtil.class);
    private static final Validator javaxValidator = Validation.buildDefaultValidatorFactory().getValidator();
    private static final SpringValidatorAdapter validator = new SpringValidatorAdapter(javaxValidator);

    public static Object validate(Object entry) {
        Errors errors = new BeanPropertyBindingResult(entry, entry.getClass().getName());
        validator.validate(entry, errors);
        if (errors.getAllErrors().isEmpty())
            return entry;
        else {
            LOGGER.error(errors.toString());
            throw new BusinessException(ResponseCode.REQUEST_PARAM_CHECK_FAILED,errors.getAllErrors().get(0).getDefaultMessage());
        }
    }

    /**
     * 是否为非0正整数
     * @param num 参数
     * @return true, false
     */
    public static boolean isPositive(BigDecimal num) {
        if (Objects.isNull(num)) {
            return false;
        }

        return BigDecimal.ZERO.compareTo(num) < 0;
    }

    /**
     * 是否大于
     * @param num1 参数1
     * @param num2 参数2
     * @return true, false
     */
    public static boolean isGreater(BigDecimal num1, BigDecimal num2) {
        if (Objects.isNull(num1) || Objects.isNull(num2)) {
            return false;
        }

        return num1.compareTo(num2) > 0;
    }

    /**
     * 是否大于等于
     * @param num1 参数1
     * @param num2 参数2
     * @return true, false
     */
    public static boolean isGreaterOrEqual(BigDecimal num1, BigDecimal num2) {
        if (Objects.isNull(num1) || Objects.isNull(num2)) {
            return false;
        }

        return num1.compareTo(num2) >= 0;
    }

    /**
     * 是否大于等于
     * @param num1 参数1
     * @param num2 参数2
     * @return true, false
     */
    public static boolean isGreaterOrEqual(Integer num1, Integer num2) {
        if (Objects.isNull(num1) || Objects.isNull(num2)) {
            return false;
        }

        return num1.compareTo(num2) >= 0;
    }

    /**
     * 是否小于
     * @param num1 参数1
     * @param num2 参数2
     * @return true, false
     */
    public static boolean isLess(BigDecimal num1, BigDecimal num2) {
        if (Objects.isNull(num1) || Objects.isNull(num2)) {
            return false;
        }

        return num1.compareTo(num2) < 0;
    }

    /**
     * 是否大于
     * @param num1 参数1
     * @param num2 参数2
     * @return true, false
     */
    public static boolean isGreater(Integer num1, Integer num2) {
        if (Objects.isNull(num1) || Objects.isNull(num2)) {
            return false;
        }

        return num1.compareTo(num2) > 0;
    }
}
